package com.wudimeng.syll.controller.api;


import com.wudimeng.syll.common.ApiResponse;
import com.wudimeng.syll.constants.Constant;
import com.wudimeng.syll.constants.Constants;
import com.wudimeng.syll.entity.ValidateCode;
import com.wudimeng.syll.entity.YongHuXinXi;
import com.wudimeng.syll.param.ClientLoginBody;
import com.wudimeng.syll.param.UpdateMimaBody;
import com.wudimeng.syll.service.IYongHuXinXiService;
import com.wudimeng.syll.utils.RedisCache;
import com.wudimeng.syll.utils.uuid.IdUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author laohei
 * @Date 2020/6/8 9:50
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("syll")
public class YongHuXinXiController {

    @Autowired
    private IYongHuXinXiService yongHuXinXiService;
    @Autowired
    private RedisCache redisCache;

    @GetMapping("/user")
    public ApiResponse getGameList(){
        try {
            return ApiResponse.instance(this.yongHuXinXiService.selectYongHuXinXi(null));
        } catch (Exception e) {
            log.error("YongHuXinXiController.getGameList.请稍后再试:" + e.getMessage());
            return ApiResponse.error(Constant.COMMON_PARAMETER_DEGRADE);
        }
    }


    /**
     * 发送验证码
     */
    @GetMapping("/captchaCode")
    public ApiResponse getcaptcha(HttpServletRequest request, HttpServletResponse response) throws ServletRequestBindingException {
        //生成验证码对象
        ValidateCode code = this.generate(new ServletWebRequest(request));
        String uuid = IdUtils.simpleUUID();;
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        code.setUuid(uuid);
        // 保存验证码信息
        redisCache.setCacheObject(verifyKey, code.getCode(), Constants.EXPIRE_SECOND, TimeUnit.MINUTES);
        return ApiResponse.instance(code);
    }

    /**
     * 登录
     * @param loginBody
     * @return
     */
    @PostMapping("/login")
    public ApiResponse login(ClientLoginBody loginBody) throws ServletRequestBindingException {
        //验证码验证
        String verifyKey = Constants.CAPTCHA_CODE_KEY + loginBody.getUuid();
        String captcha = redisCache.getCacheObject(verifyKey);
        if(StringUtils.isBlank(captcha)){
            return ApiResponse.error("验证码过期");
        }
        if(!captcha.equals(loginBody.getCode())){
            return ApiResponse.error("验证码错误");
        }
        return yongHuXinXiService.selectYongHuXinXi(loginBody);
    }

    /**
     * 注册
     * @param loginBody
     * @return
     */
//    @PostMapping("/register")
//    public ApiResponse register(ClientLoginBody loginBody) throws ServletRequestBindingException {
//        //验证码验证
//        String verifyKey = Constants.CAPTCHA_CODE_KEY + loginBody.getUuid();
//        String captcha = redisCache.getCacheObject(verifyKey);
//        if(StringUtils.isBlank(captcha)){
//            return ApiResponse.error("验证码过期");
//        }
//        if(!captcha.equals(loginBody.getCode())){
//            return ApiResponse.error("验证码错误");
//        }
//        YongHuXinXi yongHuXinXi = new YongHuXinXi();
//        yongHuXinXi.setYongHuMing(loginBody.getSheHuiXinYongDaiMa());
//        yongHuXinXi.setYongHuMiMa(loginBody.getYongHuMiMa());
//        yongHuXinXi.setLianXiDianHua(loginBody.getLianXiDianHua());
//        yongHuXinXi.setSheHuiXinYongDaiMa(loginBody.getSheHuiXinYongDaiMa());
//        try {
//            yongHuXinXiService.addYongHuXinXi(yongHuXinXi);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ApiResponse.error("注册失败");
//        }
//        return ApiResponse.success("注册成功");
//    }

    /**
     * 修改用户密码
     * @return
     * @throws ServletRequestBindingException
     */
    @PutMapping("/YongHuXinXiMima")
    public ApiResponse updYongHuXinXi(@RequestBody UpdateMimaBody updateMimaBody) throws ServletRequestBindingException {
        Map<String,String> map = new HashMap<>();
        map.put("YongHuID", updateMimaBody.getYongHuID());
        map.put("OldMiMa",updateMimaBody.getOldMiMa());
        map.put("NewMiMa",updateMimaBody.getNewMiMa());
        return yongHuXinXiService.updYongHuXinXiMima(map);
    }

    /**
     * 修改委托密码
     * @return
     * @throws ServletRequestBindingException
     */
    @PutMapping("/WeiTuoMima")
    public ApiResponse updWeiTuoMima(@RequestBody UpdateMimaBody updateMimaBody) throws ServletRequestBindingException {
        Map<String,String> map = new HashMap<>();
        map.put("WeiTuoID", updateMimaBody.getWeiTuoID());
        map.put("OldMiMa",updateMimaBody.getOldMiMa());
        map.put("NewMiMa",updateMimaBody.getNewMiMa());
        return yongHuXinXiService.updWeiTuoMima(map);
    }


    public ValidateCode generate(ServletWebRequest request) {
        String code = RandomStringUtils.randomNumeric(6);
        return new ValidateCode(code, 60);
    }
}
